package _interview150;

/**
 * 66. 加一
 */
public class No66 {
    public int[] plusOne(int[] digits) {
        int n = digits.length;
        int curSum = digits[n - 1] + 1;
        digits[n - 1] = curSum % 10;
        boolean flag = curSum / 10 > 0;
        for (int i = n - 2; i >= 0; i--) {
            curSum = digits[i] + (flag ? 1 : 0);
            digits[i] = curSum % 10;
            flag = curSum / 10 > 0;
        }
        if (flag) {
            int[] ans = new int[n + 1];
            ans[0] = 1;
            System.arraycopy(digits, 0, ans, 1, n);
            return ans;
        }
        return digits;
    }
}
